﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using wpSpeech;

namespace wpTTS_Demo
{
    public partial class MainPage : PhoneApplicationPage
    {
        //Enter AppID to initialize variable (AppID can be obtained from http://www.bing.com/developers/appids.aspx)
        private SpeechTTS SpeechServices = new SpeechTTS("##AppID##"); 
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            //Loads Speech Synthesis and text translation language codes (optional)
            SpeechServices.LoadLanguageCodes();
            //hen set to "true" (the default), Zune media playback is interrupted by speech. When set to "false," speech is concurrent with Zune media play. 
            SpeechServices.EnableZuneInterruption = false;//Zune media is not interrupted by speech.
            //Raises event when speech synthesis language codes populate.
            SpeechServices.SpokenLanguagesReady += new EventHandler(SpeechServices_SpokenLanguagesReady);
            //Raises event when translatable language codes are populated.
            SpeechServices.TranslatableLanguagesReady += new EventHandler(SpeechServices_TranslateableLanguagesReady);
            //Raises event when text translation completes.
            SpeechServices.TextTranslated += new EventHandler(SpeechServices_TextTranslated);
            //Raises event when TTS audio starts or stops.
            SpeechServices.SpeakingStatusChanged += new EventHandler(SpeechServices_SpeakingStatusChanged);
        }
        //Establishes the language codes for speech synthesis.
        //***Optional*** You can hard code any of the values found in Microsoft Translator
        void SpeechServices_SpokenLanguagesReady(object sender, EventArgs e)
        {
            lstVoices.ItemsSource = SpeechServices.SpeakLanguages;
        }
        //Establishes the language codes for translation. 
        //***Optional*** You can hard code any of the values found in Microsoft Translatorr
        void SpeechServices_TranslateableLanguagesReady(object sender, EventArgs e)
        {
            lstTranslateFrom.ItemsSource = SpeechServices.TranslatableLanguages;
            lstTranslateTo.ItemsSource = SpeechServices.TranslatableLanguages;
        }
        //The following just sets the translated text in the textbox.
        void SpeechServices_TextTranslated(object sender, EventArgs e)
        {
            textTranslateTo.Text = SpeechServices.TranslatedText;
        }
        //The follwing function allows you to determine if speech synthesis is active
        void SpeechServices_SpeakingStatusChanged(object sender, EventArgs e)
        {
            if (SpeechServices.AudioIsPlaying == true)
            {
                //Do something when application is speaking
            }
            else
            {
                //Do something when application is not speaking
            }
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnSpeak_Click(object sender, RoutedEventArgs e)
        {
            if (lstVoices.Items.Count > 0)
            {
                //Sets language to selected otherwise the default is "en"
                //You can hardcode the values (ie. 'fr' for French)
                //SpeechServices.SpeakLanguage = "fr";
                SpeechServices.SpeakLanguage = lstVoices.SelectedItem.ToString();
                SpeechServices.SpeakText(textSpeechText.Text);
            }
        }
        //Raises event when user clicks [Translate] button
        private void btnTranslate_Click(object sender, RoutedEventArgs e)
        {
            if(lstTranslateFrom.Items.Count> 0 && lstTranslateTo.Items.Count>0)
            {
                //Calls the translation method. ####.Translate(Translate from text, From Language Code, To Language Code)
                //ie. SpeechServices.Translate("Hello, World", "en", "fr"); //Translates "Hello, World" from English to French
                SpeechServices.Translate(textTranslateFrom.Text, lstTranslateFrom.SelectedItem.ToString(), lstTranslateTo.SelectedItem.ToString());
            }
        }
        //Raises event when user clicks [Speak It] button. Switches to the TTS pivot view
        private void btnSpeakIt_Click(object sender, RoutedEventArgs e)
        {
            textSpeechText.Text = textTranslateTo.Text;
            pivotMain.SelectedIndex = 0;
        }
        //Raises event when user clicks [Translate It] button. Switches to the Translate pivot view
        private void btnTranslateIt_Click(object sender, RoutedEventArgs e)
        {
            textTranslateFrom.Text = textSpeechText.Text;
            pivotMain.SelectedIndex = 1;
        }
    }
}